package cn.iocoder.yudao.module.question.service.question;

import cn.iocoder.yudao.module.question.controller.admin.questionitem.vo.QuestionItemCreateReqVO;
import cn.iocoder.yudao.module.question.convert.questionitem.QuestionItemConvert;
import cn.iocoder.yudao.module.question.dal.dataobject.questionitem.QuestionItemDO;
import cn.iocoder.yudao.module.question.dal.mysql.questionitem.QuestionItemMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.sql.Wrapper;
import java.util.*;
import cn.iocoder.yudao.module.question.controller.admin.question.vo.*;
import cn.iocoder.yudao.module.question.dal.dataobject.question.QuestionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.question.convert.question.QuestionConvert;
import cn.iocoder.yudao.module.question.dal.mysql.question.QuestionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.question.enums.ErrorCodeConstants.*;

/**
 * 试题信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class QuestionServiceImpl implements QuestionService {

    @Resource
    private QuestionMapper Mapper;

    @Resource
    private QuestionItemMapper itemMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(QuestionCreateReqVO createReqVO) {
        // 插入
        QuestionDO question = QuestionConvert.INSTANCE.convert(createReqVO);
        Mapper.insert(question);
        Long questionId = question.getId();
        List<QuestionItemDO> questionItemDOList = new ArrayList<>();
        for (QuestionItemCreateReqVO questionItemCreateReqVO: createReqVO.getQuestionItemList()) {
            QuestionItemDO itemDO = QuestionItemConvert.INSTANCE.convert(questionItemCreateReqVO);
            itemDO.setQuestionId(questionId);
            itemDO.setQuestionType(createReqVO.getQuestionType());
            questionItemDOList.add(itemDO);
        }
        itemMapper.insertBatch(questionItemDOList);
        // 返回
        return questionId;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(QuestionUpdateReqVO updateReqVO) {
        // 校验存在
        validateExists(updateReqVO.getId());
        // 更新
        QuestionDO updateObj = QuestionConvert.INSTANCE.convert(updateReqVO);
        Mapper.updateById(updateObj);

        // 删除原试题选项
        itemMapper.delete(new QueryWrapper(QuestionItemDO.builder().questionId(updateObj.getId()).build()));
        // 新增试题选项
        Long questionId = updateReqVO.getId();
        List<QuestionItemDO> questionItemDOList = new ArrayList<>();
        for (QuestionItemCreateReqVO questionItemCreateReqVO: updateReqVO.getQuestionItemList()) {
            QuestionItemDO itemDO = QuestionItemConvert.INSTANCE.convert(questionItemCreateReqVO);
            itemDO.setQuestionId(questionId);
            itemDO.setQuestionType(updateReqVO.getQuestionType());
            questionItemDOList.add(itemDO);
        }
        itemMapper.insertBatch(questionItemDOList);

    }

    @Override
    public void delete(Long id) {
        // 校验存在
        validateExists(id);
        // 删除
        Mapper.deleteById(id);
    }

    private void validateExists(Long id) {
        if (Mapper.selectById(id) == null) {
            throw exception(QUESTION_NOT_EXISTS);
        }
    }

    @Override
    public QuestionDO get(Long id) {
        return Mapper.selectById(id);
    }

    @Override
    public List<QuestionDO> getList(Collection<Long> ids) {
        return Mapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<QuestionDO> getPage(QuestionPageReqVO pageReqVO) {
        return Mapper.selectPage(pageReqVO);
    }

    @Override
    public List<QuestionDO> getList(QuestionExportReqVO exportReqVO) {
        return Mapper.selectList(exportReqVO);
    }

}
